﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESF.General.MVP;
using ESF.General;
using ESF.General.GUI;
using ESF.WinForms.Dialogs;

#endregion


namespace ESF.WinForms.Elements
{
	public class BaseForm : Form, IBaseViewer
	{
		#region Miembros

		protected IProgressDialog<IWin32Window> ProgressDialog;

		#endregion


		#region Constructor

		public BaseForm () : base ()
		{
			/*if (string.IsNullOrWhiteSpace(Title))
			{
				this.Title = ReadConfig.ApplicationName;
			}

			Title += " - versión: " + ReadConfig.ExecutingVersion;*/

			ProgressDialog = new ProgressDialog ();
			ProgressDialog.OnCancel += CancelCurrentOperation;
		}

		#endregion
		
		
		#region Miembros de IBaseViewer

		public bool frmEnabled
		{
			set { Enabled = value; }
		}


		public string frmErrorMsg
		{
			set { WinFormsHelper.ShowError (this, value); }
		}
		
		
		public string frmInfoMsg
		{
			set 
			{
				WinFormsHelper.ShowInfo (this, value);
			}
		}
		
		
		public string frmOKMsg
		{
			set 
			{
				WinFormsHelper.ShowInfo (this, value);
			}
		}


		public string frmWarnMsg
		{
			set
			{
				WinFormsHelper.ShowInfo (this, value);
			}
		}


		public string frmUserHostAddress
		{
			get 
			{
				return Log.UserHostAddress;
			}
		}

		// TODO: Soportar un modulo de autenticacion sobre windows
		public bool frmIsAuthenticated
		{
			get { return false; }
		}
		
		
		public string frmUserAlias
		{
			get { return "TODO"; }
		}


		public string frmApplicationCode
		{
			get { return ReadConfig.ApplicationCode; }
		}


		public string frmApplicationName
		{
			get { return ReadConfig.ApplicationName; }
		}


		public int CurrentAdvance100 { set { ProgressDialog.PercentageAdvance100 = value; } }

		public string CurrentStepName { set { ProgressDialog.CurrentStepName = value; } }

		#endregion


		#region Funciones
		
		protected virtual void CancelCurrentOperation () {}


		protected virtual void Clear () {}
		

		public void ShowClearProgressDialog ()
		{
			Invoke ((Action) delegate 
			{  
				ProgressDialog.Clear ();
				ProgressDialog.Show (this); 
			});
		}

		
		public void LogError (Exception pEx)
		{
			Invoke ((Action) delegate () { ProgressDialog.Hide (); });

			Log.Error (pEx);
			
			frmErrorMsg = pEx.Message;
		}


		public void CloseApplication()
		{
			WinFormsHelper.CloseApplication ();
		}


		public void SetAdvance(string pMessage = "", int pTotalWorkCount = 0, int pReadyWorkCount = 0, int pTotalErrors = 0, long pItemElapsedMs = 0)
		{
			double advance100;


			advance100 = ProcessHelper.GetAdvancePercentage100(pTotalWorkCount, pReadyWorkCount + pTotalErrors);

			CurrentStepName = 
				(string.IsNullOrEmpty(pMessage) ? string.Empty : pMessage + "... ") +
				string.Format("Procesados {0} de {1}{3}: {2:0.##}%", pReadyWorkCount, pTotalWorkCount, advance100,
				(pTotalErrors > 0 ? string.Format(" con {0} errores", pTotalErrors) : string.Empty));

			CurrentAdvance100 = (int) advance100;
		}


		public void SetAdvanceCompleted ()
		{
			CurrentAdvance100 = 100;
		}
		
		#endregion
	}
}
